package com.ymatou.iapi.productcategory.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.mongo.BuyerProductWapper;
import com.ymatou.iapi.productcategory.parameter.*;
import com.ymatou.iapi.productcategory.service.*;

/**
 * FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_SaveCategoryTag {
	private static SaveCategoryTagBean SaveCategoryTagBean;
	private static SaveCategoryTagCall SaveCategoryTagCall;
	IntergratedProductMapper ipm;
	static BuyerProductWapper BuyerProductWapper;

	@BeforeClass
	public static void setUp() throws NumberFormatException, Exception {
		Logger.createResultFile("手动录入类目标签");
		BuyerProductWapper=new BuyerProductWapper();
	}

	@Before
	public void caseUp() {
		SaveCategoryTagBean = new SaveCategoryTagBean();
		SaveCategoryTagCall = new SaveCategoryTagCall();
		try {
			
			ipm= SqlMapperFactory
					.getIntergratedProductMapper();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(SaveCategoryTagCall.getOpurl());
		BuyerProductWapper.close();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 清理类目
	 * @param cid
	 * @throws Exception
	 */
	private void cleanData(Integer cid) throws Exception {
		if(cid==null) {
			return;
		}
		BuyerProductWapper.deleteProductCatalogInfoByCid(cid);
		ipm.deleteYmtProductCategory(cid);
		ipm.deleteYMTCategoryTag(cid);
	}
	/**
	 * 初始化类目
	 * @return
	 */
	private ProductCategoryDto2 initProductCategoryDto() {
		ProductCategoryDto2 ProductCategoryDto2=new ProductCategoryDto2();
		ProductCategoryDto2.setbIsAvailable(true);
		ProductCategoryDto2.setbIsChildNodeId(true);
		ProductCategoryDto2.setCategoryEnName("CategoryEnName");
		ProductCategoryDto2.setCategoryId(10088);
		ProductCategoryDto2.setCategoryName("类目名");
		ProductCategoryDto2.setHighRisk(true);
		ProductCategoryDto2.setLevel(1);
		ProductCategoryDto2.setMasterCategory(0);
		ProductCategoryDto2.setOpeator("test");
		//url(//s2.ymatou.com/home/4aa0780b5ffeee46a01f132052fdb71a.png)
		ProductCategoryDto2.setSamplePic("http://s2.ymatou.com/home/4aa0780b5ffeee46a01f132052fdb71a.png");
		ProductCategoryDto2.setSort(1);
		ProductCategoryDto2.setType(1);
		return ProductCategoryDto2;
	}
	/**
	 * 获取一个新的CategoryId
	 * @return
	 * @throws Exception 
	 */
	private ProductCategoryDto2 getNewCategory() throws Exception {
		List<ProductCategoryDto2> productCategoryList = new ArrayList<ProductCategoryDto2>();
		SaveProductCategoryBean SaveProductCategoryBean=new SaveProductCategoryBean();
		SaveProductCategoryCall SaveProductCategoryCall=new SaveProductCategoryCall();
		ProductCategoryDto2 productCategory=initProductCategoryDto();
		//类目不能重复 删除了再加
		cleanData(productCategory.getCategoryId());
		productCategoryList.add(productCategory);
		SaveProductCategoryBean.setProductCategoryList(productCategoryList);
		SaveProductCategoryCall.setData(SaveProductCategoryBean);
		SaveProductCategoryCall.callService();
		return productCategory;
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveCategoryTag_001() {
		Logger.start(true, "手动录入类目标签");
		try {
			List tags=new ArrayList();
			 ipm.selectAllTagInfo().forEach(x->{
				 if(!tags.contains(x.get("TagId"))) {
					 tags.add(x.get("TagId"));
				 }
			});
			ProductCategoryDto2 c=getNewCategory();
			List<SaveCategoryTagDto> saveCategoryTagList=new ArrayList<SaveCategoryTagDto>();
			SaveCategoryTagDto sctd=new SaveCategoryTagDto();
			sctd.setCategoryName(c.getCategoryName());
			sctd.setTagList(tags);
			saveCategoryTagList.add(sctd);
			SaveCategoryTagBean.setSaveCategoryTagList(saveCategoryTagList);
			SaveCategoryTagCall.setData(SaveCategoryTagBean);
			SaveCategoryTagCall.callService();
			Logger.verifyEquals("200", SaveCategoryTagCall.getString("Code"), "验证Code");
			verifyDB(SaveCategoryTagBean,c.getCategoryId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	//TODO 这里需要处理多个结构 现在的case只做了单个
	private void verifyDB(SaveCategoryTagBean SaveCategoryTagBean2,Integer cid) {
		List<Map<String, Object>> list = ipm.selectYMTCategoryTag2(cid);
		Logger.verifyEquals(SaveCategoryTagBean2.getSaveCategoryTagList().get(0).getTagList().size(), list.size(), "验证size");
		list.forEach(x->{
			Logger.verifyEquals(true, SaveCategoryTagBean2.getSaveCategoryTagList().get(0).getTagList().contains(x.get("TagId")), "验证存在数据");
			
		});
	}
}